﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.AssignDataFromTC.FilterWnd
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.AssignDataFromTC.ADTCExtensions;
using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Threading;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.AssignDataFromTC;

public class FilterWnd : TxPopupWindow, IComponentConnector
{
  internal TextBox _searchTextBox;
  internal Button _searchButton;
  private bool _contentLoaded;

  public event EventHandler<EventArgs> FilterSet;

  public FilterWnd() => this.InitializeComponent();

  public bool ShouldClose { get; set; }

  public int ColumnIndex { get; set; } = 1;

  public string SearchText { get; set; } = "";

  private void SetPosition(FilterButton filterButton)
  {
    DataGridColumnHeader vtAncestor = ADTCExtension.GetVTAncestor<DataGridColumnHeader>((object) filterButton);
    if (vtAncestor == null)
      return;
    Rect rect = vtAncestor.TransformToVisual((Visual) vtAncestor).TransformBounds(new Rect(vtAncestor.RenderSize));
    Point screen = vtAncestor.PointToScreen(rect.BottomLeft);
    Matrix transformToDevice = PresentationSource.FromVisual(Application.Current == null || Application.Current.MainWindow == null ? (Visual) ((Window) this).Owner : (Visual) Application.Current.MainWindow).CompositionTarget.TransformToDevice;
    double m11 = transformToDevice.M11;
    double m22 = transformToDevice.M22;
    ((Window) this).Top = screen.Y / m22;
    ((Window) this).Left = screen.X / m11;
  }

  public void InitializeAndShow(int columnIndex, FilterButton filterButton)
  {
    this.ColumnIndex = columnIndex;
    this.SetPosition(filterButton);
    ((UIElement) this).IsEnabled = true;
    if (((FrameworkElement) this).IsLoaded)
      this._searchTextBox.Text = this.SearchText = "";
    ((Window) this).Show();
    this._searchTextBox.Focus();
  }

  private void HideAndDisable()
  {
    ((Window) this).Hide();
    if (((Window) this).Owner != null && !((Window) this).Owner.IsActive)
      ((DispatcherObject) this).Dispatcher.InvokeAsync((Action) (() => ((Window) this).Owner.Activate()));
    ((UIElement) this).IsEnabled = false;
  }

  protected virtual void OnDeactivate()
  {
    if (this.ShouldClose || !((UIElement) this).IsVisible)
      return;
    this.HideAndDisable();
  }

  private void FilterWnd_Closing(object sender, CancelEventArgs e)
  {
    if (this.ShouldClose)
    {
      ((Window) this).Close();
    }
    else
    {
      this.HideAndDisable();
      e.Cancel = true;
    }
  }

  private void Filter()
  {
    this.SearchText = this._searchTextBox.Text;
    this.HideAndDisable();
    EventHandler<EventArgs> filterSet = this.FilterSet;
    if (filterSet == null)
      return;
    filterSet((object) this, new EventArgs());
  }

  private void searchButton_Click(object sender, RoutedEventArgs e) => this.Filter();

  protected virtual void OnKeyUp(KeyEventArgs e)
  {
    if (e.Key == Key.Return)
    {
      e.Handled = true;
      this.Filter();
    }
    else
    {
      // ISSUE: explicit non-virtual call
      __nonvirtual (((UIElement) this).OnKeyUp(e));
    }
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/assigndatafromtc/cuiadtcfilterwnd.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        ((Window) target).Closing += new CancelEventHandler(this.FilterWnd_Closing);
        break;
      case 2:
        this._searchTextBox = (TextBox) target;
        break;
      case 3:
        this._searchButton = (Button) target;
        this._searchButton.Click += new RoutedEventHandler(this.searchButton_Click);
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
